package com.zhang.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.backend.entity.FamilyTree;
import com.zhang.backend.entity.Person;
import com.zhang.backend.mapper.FamilyTreeMapper;
import com.zhang.backend.mapper.PersonMapper;
import com.zhang.backend.service.FamilyTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;

@Service
@Transactional
public class FamilyTreeServiceImpl extends ServiceImpl<FamilyTreeMapper, FamilyTree> implements FamilyTreeService {
    @Autowired
    private PersonMapper personMapper;

    @Override
    public boolean removeById(Serializable id) {
        UpdateWrapper<Person> updateWrapper = new UpdateWrapper<>();
        // 【修正】使用了正确的数据库列名 "family_id" 替换错误的 "ParentFamilyID"
        // 注意：这里我们假设Person实体中没有定义外键关联的字段，直接操作数据库列
        updateWrapper.eq("family_id", id).set("family_id", null);
        personMapper.update(null, updateWrapper);

        // 然后再安全地删除这个家庭
        return super.removeById(id);
    }
}